package com.xingkeduo.util;

import com.xingkeduo.configuration.druid.OperationDruidDbConfig;
import com.xingkeduo.model.dto.ShopSkuInfoDto;
import org.slf4j.Logger;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-13 上午11:17
 */

public class TimeUtil {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(TimeUtil.class);

    public static Date getDateOfMonday(String today, int afterNum) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(today)) {
            Date date = new Date();
            return getDateOfMonday(date, afterNum);
        } else {
            Date date = null;
            try {
                date = format.parse(today);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return getDateOfMonday(date, afterNum);
        }
    }

    /**
     * @return java.util.Date
     * @description 获取传入时间的当前周一日期
     * @Param [date, afterNum]
     **/
    public static Date getDateOfMonday(Date date, int afterNum) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        //设置一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        cal.add(Calendar.DATE, 7 * afterNum);
        return cal.getTime();
    }

    public static int getWeekDayForDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        day--;
        return day > 0 ? day : 7;
    }

    /**
     * @return java.lang.String
     * @description 获取传入时间的当前周日日期
     * @Author 情绪极其不稳定的Shadow
     * @Param [date]
     **/
    public static Date getDateOfSunday(Date date) {
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(cal.DATE) + 6);
        return cal.getTime();
    }

    public static Date FormatTime(Date date) {
        SimpleDateFormat simdf = new SimpleDateFormat("HH:mm:ss");
        String formatDate = simdf.format(date);
        Date newDate = null;
        try {
            newDate = simdf.parse(formatDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * @param oneTime 指定时间
     * @param twoTime 指定时间之后的时间or指定时间之前的时间
     * @param minutes 时间间隔
     * @return java.util.List<java.lang.String>
     * @description 根据 minutes 时间间隔获取 向前or向后获取对应时间间隔
     * @Author 情绪极其不稳定的Shadow
     **/
    public static List<String> getTimeInterval(String oneTime, String twoTime, int minutes) {
        System.out.println("minutes--->>>"+minutes);
        List<String> timeInterval = new ArrayList<>();
        SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");
        Date compareTime = null;
        try {
            compareTime = SDF.parse(twoTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date startTime = null;
        try {
            startTime = SDF.parse(oneTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        timeInterval.add(SDF.format(startTime));

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startTime);

        if (compareTime.before(startTime)) {
            while (!startTime.before(compareTime)) {
                startTime = dealTime(timeInterval, startTime, rightNow, minutes * -1);
            }
            return timeInterval;
        } else {
            while (!compareTime.before(startTime)) {
                startTime = dealTime(timeInterval, startTime, rightNow, minutes);
            }
            return timeInterval;
        }
    }

    private static Date dealTime(List<String> timeInterval, Date startTime, Calendar rightNow, int minutes) {
        SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");
        rightNow.add(Calendar.MINUTE, minutes);
        startTime = rightNow.getTime();
        timeInterval.add(SDF.format(startTime));
        return startTime;
    }

    /**
     * @return java.lang.String
     * @description 根据指定时间获取当前周几
     * @Author 情绪极其不稳定的Shadow
     * @Param [date]
     **/
    public static String getWeekTime(String prepareDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
        Date dateYear = new Date();
        String formatDate = formatYear.format(dateYear);
        Date date = null;
        try {
            date = format.parse(formatDate + "-" + prepareDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    public static String getFetureDate(int past, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * @return java.util.Date
     * @description
     * @Author 情绪极其不稳定的Shadow
     * @Param [date, time]
     **/
    public static Date joinDateAndTime(String date, String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sysYear = getSysYear();
        Date parse = null;
        try {
            parse = format.parse(sysYear+"-"+date+" "+time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date formatDate(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dateYear = null;
        try {
            dateYear = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateYear;
    }

    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }

    /**
     * @description  String yyyy-MM-dd HH:mm:ss 类型转换为10位时间戳
     * @Author 情绪极其不稳定的Shadow
     * @Param [time]
     * @return java.lang.Integer
     **/
    public static Integer DateToTimestamp(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parseDate = null;
        try {
            parseDate = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Timestamp ts = new Timestamp(parseDate.getTime());
        return (int) ((ts.getTime())/1000);
    }

    /**
     * @description  根据日期字符串 获取是 今天 or 明天 or 具体日期时间
     * @Author 情绪极其不稳定的Shadow
     * @Param [str, formatStr]
     * @return java.lang.String
     **/

    public static String isToday(String str, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = longOfTwoDate(date, new Date());
        if(i == 0){
            return "今天";
        }else if (i == 1){
            return "明天";
        }
        return str;
    }

    /**
     * 用于计算两个日期之间的间隔 （原时间差工具类跨年计算错误）
     * @param first 第一个日期(取最近的时间)
     * @param second 第二个日期（较远的时间）
     * @return 返回天数差
     *
     */
    public static int  longOfTwoDate(Date first, Date second){
        /**
         * 原有的时间在转化成String然后再次解析为Date
         * 目的: 清除时间原本自带的小时毫秒便于计算
         */
        //格式化时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //将时间转化成String类型
        String fDate = format.format(first);
        String sDate = format.format(second);
        //解析String为Date
        Date date1 = null;
        try {
            date1 = format.parse(fDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = format.parse(sDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //两日期相减，所得毫秒数 除以1000得秒 除以3600得小时 除24得天数得到两个日期的间隔
        int day = (int) ((date1.getTime() - date2.getTime()) / (1000*3600*24));
        //返回天数
        return day;
    }

    /**
     * @description  按照对应格式转换  Date --->>> String
     * @Author 情绪极其不稳定的Shadow
     * @Param [date, formatStr]
     * @return java.lang.String
     **/
    public static String DateToFormat(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }

    /**
     * 计算两天间隔
     * @param fDate
     * @param oDate
     * @return
     * @author 中文输入法发挥不稳定的刘川
     * @date 19-5-8 下午6:30
     */
    public static int getTwoDayLength(Date fDate,Date oDate){
    int days=(int)((oDate.getTime()-fDate.getTime())/(1000*3600*24));
    return days;
    }

    /**
     * 通过传入的时间，计算包含传入时间在内的5天日期
     * @author 中文输入法发挥不稳定的刘川
     * @date 19-5-8 下午8:16
        * @param now
     * @return java.util.List<java.util.Date>
     */
    public static List<Date> getAfterFiveDayByFirstDay(Date now){
        List<Date> dateList=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(now);
        dateList.add(now);
        for(int i=0;i<4;i++){
            calendar.add(Calendar.DATE,1);
            dateList.add(calendar.getTime());
        }
        return dateList;
    }

    /**
     * @description  String 转 Date 格式 yyyy-MM-dd HH:mm:ss
     * @Author 情绪极其不稳定的Shadow
     * @Param [dateTime]
     * @return java.util.Date
     **/
    public static Date stringToFormatDate(String dateTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = format.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * @description 时间戳转换成时间String类型
     * @Author 情绪极其不稳定的Shadow
     * @Param [time]
     * @return java.lang.String
     **/
    public static String timestampToDate(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    public static void main(String[] args) {
        System.out.println(!"07-02".equals("07-04"));
    }


    public static Long getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return min;
    }
    //传入时间和当前日期比较，返回正值是代表左侧日期大于参数日期，反之亦然，日期格式必须一致
    public static int compareData(String date){
        Date dataNew=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String ds=df.format(dataNew);
        return ds.compareTo(date);
    }


}



